﻿using System.Web;
using System.Web.Mvc;

namespace Super.Common
{
    /// <summary>
    /// 页面输出消息类封装
    /// <remarks>Last Update 20111130@Richwong</remarks>
    /// </summary>
    public class PageMessage
    {
        /// <summary>
        /// 关弹出层(窗)并且输出表单保存状态的信息脚本 
        /// </summary>
        /// <param name="content">可自定义内容</param>
        /// <param name="refurl">可以自定义刷新页面的地址，留空为刷新当前页面</param>
        /// <param name="type">alert，success，error，confirm,warning</param>
        /// <returns></returns>
        public static string ShowFormMessage(string content = "恭喜！数据保存成功,正在刷新数据...", string refurl = "",
                                             string type = "success")
        {
            string code = "parent.showFormMessage('" + content + "','" + refurl + "','" + type + "');";
            // code = "if(parent.document.getElementById('append_parent')){parent." + code + "}else{" + code + "}";
            return JavaScriptFormat(code); //这是弹出层的表单，必须调用父页面的
        }

        /// <summary>
        /// 输出一个显示提示信息的脚本
        /// </summary>
        /// <param name="tips"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static string ShowPageTips(string tips, string type = "success")
        {
            string code = "showTips('" + tips + "','" + type + "');";
            code = "if(!parent.document.getElementById('append_parent')){parent." + code + "}else{" + code + "}";
            return JavaScriptFormat(code);
        }

        /// <summary>
        /// 输出一个显示浮动信息的脚本
        /// </summary>
        /// <param name="content"></param>
        /// <param name="type">alert，success，error，confirm,warning</param>
        /// <param name="close"> </param>
        /// <param name="refurl"> </param>
        /// <returns></returns>
        public static string ShowPageMessage(string content, string type = "alert", bool close = true,
                                             bool refurl = true)
        {
            string script = "";
            if (close)
            {
                script += "parent.$.close('bangke_editdialog_box');/r/n";
                script += "parent.showMessage('" + content + "','" + type + "');";
            }
            else
            {
                script += "parent.showMessage('" + content + "','" + type + "');";
            }

            return JavaScriptFormat(script);
        }


        private static string _Close_Bangke_Editdialog()
        {
            return "parent.$.close('bangke_editdialog_box');";
        }

        /// <summary>
        /// 封装返回js格式
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public static string JavaScriptFormat(string code)
        {
            return (string.IsNullOrEmpty(code))
                       ? ""
                       : string.Format("<script type='text/javascript' language='javascript'>{0}</script>", code);
        }

        /// <summary>
        /// 输出一个显示浮动窗的脚本
        /// </summary>
        /// <param name="content"></param>
        /// <param name="title"></param>
        /// <param name="wdith"></param>
        /// <param name="height"></param>
        /// <returns></returns>
        public static string ShowPageDialog(string content, string title = "", int wdith = 500, int height = 500)
        {
            string code = "showDialog('" + content + "','" + title + "'," + wdith + "," + height + ");";
            code = "if(!parent.document.getElementById('append_parent')){parent." + code + "}else{" + code + "}";
            return JavaScriptFormat(code);
        }

        /// <summary>
        /// 操作返回来源页面，tips不等于空就输出消息
        /// </summary>
        /// <param name="c">在控制器中，用this表示</param>
        /// <param name="tips"></param>
        /// <returns></returns>
        public static RedirectResult ShowReferrerPage(Controller c, string tips = "恭喜！操作成功！")
        {
            return ShowReferrerPage(c, tips, "success");
        }

        public static RedirectResult ShowReferrerPage(Controller c, string tips, string type = "success", string refurl = "")
        {
            refurl = (refurl != "") ? refurl : HttpContext.Current.Request.UrlReferrer.ToString();

            if (!string.IsNullOrEmpty(tips))
                c.TempData["PageShowMessages"] = ShowPageTips(tips, type);

            if (refurl.Length > 0)
                return new RedirectResult(refurl);

            string[] currentUrl = HttpContext.Current.Request.Url.Segments;
            return new RedirectResult(currentUrl[0] + currentUrl[1]);
        }

        public static void BkMessage(Controller c, string content, string title = "", string time = "3000")
        {
            string code = "bkMessage('" + content + "','" + title + "'," + time + ");";

            code = "if(!parent.document.getElementById('append_parent')){parent." + code + "}else{" + code + "}";

            c.ViewBag.PageShowMessages = JavaScriptFormat(code);
        }
    }
}